/*
 * @(#)DocumentMessageFormatter.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved.
 * 
 *  This software is the confidential and proprietary information of DCIVision Ltd 
 *  ("Confidential Information"). You * shall not disclose such Confidential
 *   Information and shall use it only in accordance with the terms of the 
 *   license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.sql.Connection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.alert.bean.UpdateAlertType;
import com.dcivision.alert.dao.UpdateAlertTypeDAObject;
import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;

/**
 * <p>
 * Class Name: DocumentMessageFormatter.java
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-11-06
 */
public class DocumentMessageFormatter extends AbstractMessageFormatter {
  
  private static final Log log = LogFactory.getLog(DocumentMessageFormatter.class);

  private static final String     FILE_SEPARATOR = "\\";

  private UpdateAlertTypeDAObject updateAlertTypeDAO;

  private UpdateAlertType         updateAlertType;

  public DocumentMessageFormatter(UpdateAlertSystemLog bean, SessionContainer sessionContainer,
      Connection dbConn) throws ApplicationException {
    super(bean, sessionContainer, dbConn);
  }

  protected void makeMessageType() {
    if (UpdateAlert.INVITE_TYPE.equals(getActionType())
        || UpdateAlert.DISCUSS_ACTION.equals(getActionType()) 
        ||UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(updateAlertSystemLog.getActionType())
        ||UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(updateAlertSystemLog.getActionType())
        ||UpdateAlert.DOCUMENT_INVITATION_UPDATE_DUEDATE.equals(updateAlertSystemLog.getActionType())) {

      setMessageType("documentInvitation");

    }
    else {

      setMessageType("documentNotification");
    }
  }

  public String getSender() {
    String str = "";
    if (!(UpdateAlert.INVITE_TYPE.equals(updateAlertSystemLog.getActionType()) || UpdateAlert.DISCUSS_ACTION.equals(updateAlertSystemLog.getActionType())
        || UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(updateAlertSystemLog.getActionType()) || UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(updateAlertSystemLog.getActionType()))) {
      str = "System";
    }
    else {
      str = userName;
    }
    return str;
  }

  public String getSubject() {
    String str = "";
    String isList = this.isList() ? "yes" : "";
    str = "<a class=\"strongllink\" href=\"#\" "
        + " title = \" "
        + getSubjectAsTitle()
        + " \" "
        + "onClick=\"OpenCenteredPopup('./EditMessageDetail.do?"
        + "navMode="
        +GlobalConstant.NAV_MODE_VIEW
        + "&messageType="
        + getMessageType()
        + "&objectid="
        + getObjectID().toString()
        + "&systemLogID="
        + updateAlertSystemLog.getID()
        + "&isList="
        + isList
        + "', 'DCIVisionViewWorkflowProgress', '850', '360', 'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no');\">";
    str += getProperLengSubject();
    str += "</a>";

    return str;
  }

/**
 * 
 * @return
 */
  protected Integer getObjectID() {
    if(Utility.isEmpty(updateAlertSystemLog.getRefObjectID())
      ||UpdateAlert.DOCUMENT_INVITATION_CANCEL.equals(updateAlertSystemLog.getActionType())
      ||UpdateAlert.DOCUMENT_INVITATION_EXPIRED.equals(updateAlertSystemLog.getActionType())
      ||UpdateAlert.DOCUMENT_INVITATION_UPDATE_DUEDATE.equals(updateAlertSystemLog.getActionType())){
      return updateAlertSystemLog.getObjectID();
      }
    return updateAlertSystemLog.getRefObjectID();
  }
/**
 * 
 * @throws ApplicationException
 */
  protected void makeDocument() throws ApplicationException {
    String objectType = getObjectType();
    String actionType = getActionType();
    DmsDocument dmsDocument = new DmsDocument();

    DmsDocument parDocument = new DmsDocument();
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, dbConn);
    dmsDocument = docRetrievalManager.getDocument(getObjectID());

    String docName = "";
    String folderName = "";

    //This null checking is defensive and in favour of fail-over approach since database data tables relationship are weakly referenced 
    //with no foreign key.
    if (dmsDocument != null) {
      parDocument = docRetrievalManager.getDocument(dmsDocument.getParentID());
      docName = dmsDocument.getDocumentName();
      folderName = parDocument.getDocumentName();
    } else {
      log.warn("DmsDocument is null with lookup objectId=" + getObjectID());
    }

    if (UpdateAlert.CREATE_ACTION.equals(actionType) || UpdateAlert.CREATE_SHORTCUT_ACTION.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.create", docName, folderName, userName);
    } else if (UpdateAlert.DELETE_ACTION.equals(actionType) || UpdateAlert.DELETE_SHORTCUT_ACTION.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.delete", docName, folderName, userName);
    } else if (UpdateAlert.UPDATE_ACTION.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.update", docName, folderName, userName);
    } else if (UpdateAlert.MOVE_ACTION.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.move", docName, folderName);
    } else if (UpdateAlert.MODIFY_RELATED_DOC.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.modify_related_doc", docName, folderName);
    } else if (UpdateAlert.VIEW_ACTION.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.view", docName, folderName, userName);
    } else if (UpdateAlert.AUTOMATE_CHECKIN.equals(objectType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.auto_check_in", docName, folderName);
    } else if (UpdateAlert.REMIND_ACTION.equals(actionType)) {
      this.messageSubject = this.getMessageResource("home.message.document_notification.remind", docName, folderName);
    }

  }


  protected String getFolderPath(DocumentRetrievalManager docRetrievalManager, Integer sParentID,
      DmsDocument rootDocument) throws ApplicationException {

    StringBuffer buffer = new StringBuffer();
    DmsDocument doc;
    do {
      doc = docRetrievalManager.getDocument(sParentID);

      if (rootDocument == null) {
        buffer.insert(0, GlobalConstant.EMPTY_FIELD);
        break;
      }
      if (DmsOperationConstant.DMS_PERSONAL_ROOT.equals(rootDocument.getDocumentName())) {
        // For Personal Folder
        String name = doc.getDocumentName();
        if (rootDocument.getOwnerID().intValue() != sessionContainer.getUserRecordID().intValue()) {
          name = UserInfoFactory.getUserFullName(doc.getOwnerID()) + " " + doc.getDocumentName();
        }
        buffer.insert(0, name);
      }
      else {
        // For Public Folder
        buffer.insert(0, doc.getDocumentName());
      }
      buffer.insert(0, FILE_SEPARATOR);
      sParentID = doc.getParentID();
    }
    while (sParentID.intValue() != 0);

    String path = buffer.toString();
    if (path.length() > 0) {
      path = path.substring(1);
    }

    return path;
  }

  protected void setUserDefinedInviteSubject() {
    this.messageSubject = this.updateAlertType.getAlertSubject();
  }

  protected void setSystemMakeSubject() throws ApplicationException {
    this.updateAlertTypeDAO = new UpdateAlertTypeDAObject(sessionContainer, dbConn);
    this.updateAlertType = (UpdateAlertType) updateAlertTypeDAO
        .getUpdateAlertType(this.updateAlertSystemLog.getUpdateAlertTypeID(), null); // EIP-1174 07/01/08 LEE UPDATE
        //.getObjectByID(this.updateAlertSystemLog.getUpdateAlertTypeID()); // EIP-1174 07/01/08 LEE REMOVE
    if (UpdateAlert.INVITE_ACTION.equals(getActionType())
        && !Utility.isEmpty(this.updateAlertType.getAlertSubject())) {
      setUserDefinedInviteSubject();
    }
    else {
      makeDocument();
    }
  }

  public String getUrlLink() {
    String str = "";
    String isList = this.isList() ? "yes" : "";
    str = "EditMessageDetail.do?"
        + "messageType="
        + getMessageType()
        + "&objectid="
        + getObjectID().toString()
        + "&systemLogID="
        + updateAlertSystemLog.getID()
        + "&isList="
        + isList;

    return str;
  }
}
